/**
 * Copyright (c) 2011-2014, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

package com.jfinal.weixin.sdk.api;

import java.util.Map;

import org.apache.log4j.Logger;

import com.jfinal.weixin.sdk.utils.HttpUtils;

import cn.com.longfuinfo.wx.exception.SaveException;

/**
 * 模板消息 API
 * 文档地址：http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html
 */
public class TemplateMsgApi {
	private static Logger log = Logger.getLogger(MsgApi.class);
	private static String sendApiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";

	
	
	public static ApiResult editWX_send(Map<String,Object> mapParam) throws SaveException{
		
		try {
			log.info("OPENID:::::"+mapParam.get("OPENID"));
			//String jsonParam = "{\"touser\":\""+mapParam.get("OPENID")+"\",\"msgtype\": \"text\",\"text\": {\"content\": \""+mapParam.get("ANSWER_CONTENT")+"\"}}";
			String jsonParam = "{\"touser\":\""+mapParam.get("OPENID")+"\",\"template_id\":\""+"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY"+"\",\"data\": { \""+mapParam.get("ANSWER_CONTENT")+"\"}}";
			log.info("jsonParam::::"+jsonParam);
			return send(jsonParam);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new SaveException(e.getMessage(), e);
		}
	}
	/**
	 * 发送模板消息
	 */
	public static ApiResult send(String jsonStr) {
		
		String jsonResult = HttpUtils.post(sendApiUrl + AccessTokenApi.getAccessToken().getAccessToken(), jsonStr);
		return new ApiResult(jsonResult);
	}
}


